package object.environments {
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.events.AssetEvent;
	import away3d.library.assets.AssetType;
	import away3d.lights.DirectionalLight;
	import away3d.loaders.Loader3D;
	import away3d.materials.lightpickers.StaticLightPicker;
	import away3d.materials.methods.TerrainDiffuseMethod;
	import away3d.materials.TextureMaterial;
	import away3d.primitives.PlaneGeometry;
	import away3d.textures.BitmapTexture;
	
	/**
	 * 车库环境
	 */
	public class Garage extends ObjectContainer3D {
		[Embed(source="../../assets/garage/FloorMap.jpg")]
		private var FloorMapAsset:Class;
		[Embed(source="../../assets/garage/GarageMap.jpg")]
		private var GarageMapAsset:Class;
		[Embed(source="../../assets/garage/FloorShadowMap.png")]
		private var FloorShadowMapAsset:Class;
		[Embed(source="../../assets/garage/Garage.3DS",mimeType="application/octet-stream")]
		private var GarageAsset:Class;
		
		private var _garageMesh:Mesh;
		private var _floorMesh:Mesh;
		private var _floorShadowMesh:Mesh;
		
		public function Garage(light:DirectionalLight) {
			y = 188;
			//地板材质
			var floorTexture:BitmapTexture = new BitmapTexture(new FloorMapAsset().bitmapData);
			var floorShadowTexture:BitmapTexture = new BitmapTexture(new FloorShadowMapAsset().bitmapData);
			var floorMater:TextureMaterial = new TextureMaterial(floorTexture, true, true);
			floorMater.specular = 1;
			floorMater.lightPicker = new StaticLightPicker([light]);
			floorMater.diffuseMethod = new TerrainDiffuseMethod([floorTexture], floorShadowTexture, [10]);
			//地板阴影材质
			var floorShadowMater:TextureMaterial = new TextureMaterial(floorShadowTexture);
			floorShadowMater.alpha = 0.9;
			//地板模型
			_floorMesh = new Mesh(new PlaneGeometry(3800, 3800), floorMater);
			_floorMesh.y = -238;
			_floorMesh.rotationY = -95;
			addChild(_floorMesh);
			//地板阴影模型
			_floorShadowMesh = new Mesh(new PlaneGeometry(3800, 3800), floorShadowMater);
			_floorShadowMesh.y = -237;
			_floorShadowMesh.rotationY = -95;
			addChild(_floorShadowMesh);
			
			var loader:Loader3D = new Loader3D();
			loader.loadData(new GarageAsset(), null, null);
			loader.addEventListener(AssetEvent.ASSET_COMPLETE, onLoaderAssetComplete);
		}
		
		private function onLoaderAssetComplete(e:AssetEvent):void {
			if (e.asset.assetType == AssetType.MESH) {
				_garageMesh = e.asset as Mesh;
				_garageMesh.material = new TextureMaterial(new BitmapTexture(new GarageMapAsset().bitmapData), true);
				_garageMesh.roll(180);
				_garageMesh.geometry.scale(3);
				addChild(_garageMesh);
			}
		}
	}
}